package com.taijihuabao.common.activititasklistener.reissuecardtasklistener;

import com.taijihuabao.modules.entity.ApprovalEntity;
import com.taijihuabao.modules.entity.DeptUserEntity;
import com.taijihuabao.modules.enums.optenums.dept.DeptRoleEnum;
import com.taijihuabao.modules.mapper.ApprovalMapper;
import com.taijihuabao.modules.mapper.DeptMapper;
import com.taijihuabao.modules.mapper.DeptUserMapper;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 主管会签人员
 *
 * @author Tu
 * @date 2022/1/13 16:28
 */
@Component
public class AdminCountersignListener implements ExecutionListener {

    @Resource
    private ApprovalMapper approvalMapper;
    @Resource
    private DeptUserMapper deptUserMapper;
    @Resource
    private DeptMapper deptMapper;

    @Override
    public void notify(DelegateExecution execution) {
        //获取businessKey
        String businessKey = execution.getProcessInstanceBusinessKey();
        //获取审批数据
        ApprovalEntity approvalEntity = approvalMapper.selectById(Long.valueOf(businessKey));
        //查询申请人id查询本部门审批人
        List<DeptUserEntity> applys = deptUserMapper.selectDeptByUserId(approvalEntity.getApplyUserId());
        String deptCode = applys.get(0).getDeptCode();
        List<DeptUserEntity> users = deptUserMapper.selectDeptByDeptCode(deptCode);
        List<String> continueList = new ArrayList<>();
        //查询父级部门主管
        for (DeptUserEntity due : users) {
            if (DeptRoleEnum.Assessor.getCode().equals(due.getDeptRole())) {
                //查询审核员连续主管
                List<DeptUserEntity> allPersons = deptUserMapper.selectDeptByUserId(due.getUserId());
                String assessorDept = allPersons.get(0).getDeptCode();
                String parentDept = deptMapper.selectDeptByCode(assessorDept).getParentDept();
                List<DeptUserEntity> parentDeptPerson = deptUserMapper.selectDeptByDeptCode(parentDept);
                //遍历部门人员查出父部门审核员
                if (CollectionUtils.isNotEmpty(parentDeptPerson)) {
                    boolean adminExist = false;
                    for (DeptUserEntity continueUser : parentDeptPerson) {
                        if (DeptRoleEnum.Admin.getCode().equals(continueUser.getDeptRole())) {
                            //如果主管和审批人是同一人，去重
                            Long continueUserId = continueUser.getUserId();
                            if (!continueUserId.equals(due.getUserId())) {
                                //添加会签人员
                                adminExist = true;
                                continueList.add(String.valueOf(continueUser.getUserId()));
                            }
                        }
                    }
                    if (!adminExist) {
                        //父部门无主管设置上一节点审批人
                        continueList.add(String.valueOf(due.getUserId()));
                    }
                } else {
                    //无父部门或部门没有人
                    continueList.add(String.valueOf(due.getUserId()));
                }
                //设置会签人员
                execution.setVariable("continueList", continueList);
                break;
            }
        }
    }

}
